package net.ghy.statistics.ages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import dao.hb.BxMidle;
import dao.hb.UUser;

import net.bat.db.BaseDAO;
import net.bat.db.BaseDAOImpl;
import net.ghy.statistics.utils.CollectiontoString;
import net.ghy.statistics.utils.EUtils;

public class AgesExpData {
	double initdata = 0.0;
	static private BaseDAO dao;
	public BaseDAO getDao() {
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		return dao;
	}

	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}
	//将在职员工分年龄段统计
	public Map<String,List> zzAgesbxData(String start, String end){
		Map ages_bx = new TreeMap<String,List>();
		List zzThirty = new ArrayList();
		List zzForty = new ArrayList();
		List zzFifty = new ArrayList();
		List zzSixty = new ArrayList();
		List zzOverSixty = new ArrayList();
        List<HashMap> hm ;
        String wkState= "在职";
        BxMidle agesbgf = new BxMidle(0.0, 0.0, 0.0, 0.0);
        String hql = "from BxMidle b,UUser u where b.wkState='在职' and u.id = b.uid and (b.bxtype='门诊 ' or b.bxtype='住院')"+ " and ( b.dtLook >= '" + start + "' and b.dtLook <='"
				+ end + "') order by b.dtLook ";
        List ubx = getDao().find(hql);
        for (int i = 0 ; i < ubx.size(); i ++){
        	Object[] o = (Object[]) ubx.get(i);
        	UUser u = (UUser) o[1];
        	if(EUtils.getAge(u.getId())<=30){
        		zzThirty.add(u.getId());
        	}else if(EUtils.getAge(u.getId())<=40){
        		zzForty.add(u.getId());
        	}else if(EUtils.getAge(u.getId())<=50){
        		zzFifty.add(u.getId());
        	}else if(EUtils.getAge(u.getId())<=60){
        		zzSixty.add(u.getId());
        	}else
        		zzOverSixty.add(u.getId());
        	
        }
        if(start.contains("01-01")){
            ages_bx.put("30及以下", getAgesDataBgf(zzThirty,start,end,wkState));
            ages_bx.put("31-40", getAgesDataBgf(zzForty,start,end,wkState));
            ages_bx.put("41-50", getAgesDataBgf(zzFifty,start,end,wkState));
            ages_bx.put("51-60", getAgesDataBgf(zzSixty,start,end,wkState));
            ages_bx.put("60以上", getAgesDataBgf(zzOverSixty,start,end,wkState));
        }else{
		    ages_bx.put("30及以下", getAgesData(zzThirty,start,end,wkState));
		    ages_bx.put("31-40", getAgesData(zzForty,start,end,wkState));
		    ages_bx.put("41-50", getAgesData(zzFifty,start,end,wkState));
		    ages_bx.put("51-60", getAgesData(zzSixty,start,end,wkState));
		    ages_bx.put("60以上", getAgesData(zzOverSixty,start,end,wkState));
        }
		return ages_bx ;
	}
	//将退休员工分年龄段统计
	public Map<String,List> txAgesbxData(String start, String end){
		Map ages_bx = new TreeMap<String,List>();
		List txBelowSixty = new ArrayList();
		List txSixty = new ArrayList();
		List txSeventy = new ArrayList();
		List txOverEighty = new ArrayList();
		String wkState= "退休";
        List<HashMap> hm ;
        String hql = "from BxMidle b,UUser u where b.wkState='退休' and u.id = b.uid and (b.bxtype='门诊 ' or b.bxtype='住院')"+ " and ( b.dtLook >= '" + start + "' and b.dtLook <='"
				+ end + "') order by b.dtLook ";
        List ubx = getDao().find(hql);
        for (int i = 0 ; i < ubx.size(); i ++){
        	Object[] o = (Object[]) ubx.get(i);
        	UUser u = (UUser) o[1];
        	if(EUtils.getAge(u.getId())<=60){
        		txBelowSixty.add(u.getId());
        	}else if(EUtils.getAge(u.getId())<=70){
        		txSixty.add(u.getId());
        	}else if(EUtils.getAge(u.getId())<=80){
        		txSeventy.add(u.getId());
        	}else
        		txOverEighty.add(u.getId());
        	
        }
        if(start.contains("01-01")){
        	ages_bx.put("60及以下", getAgesTxDataBgf(txBelowSixty,start,end,wkState));
        	ages_bx.put("61-70", getAgesTxDataBgf(txSixty,start,end,wkState));
        	ages_bx.put("71-80", getAgesTxDataBgf(txSeventy,start,end,wkState));
        	ages_bx.put("80以上", getAgesTxDataBgf(txOverEighty,start,end,wkState));
        }else{
        	ages_bx.put("60及以下", getAgesData(txBelowSixty,start,end,wkState));
        	ages_bx.put("61-70", getAgesData(txSixty,start,end,wkState));
        	ages_bx.put("71-80", getAgesData(txSeventy,start,end,wkState));
        	ages_bx.put("80以上", getAgesData(txOverEighty,start,end,wkState));
        }
		return ages_bx ;
	}
	public Map<String, BxMidle> getAgeTjData(List uids, String type,String start,String end,String wkState) {
		Map<String, BxMidle> agesBx = new HashMap<String, BxMidle>();
		BxMidle agestj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		if (uids.size() > 0) {
			String uid = CollectiontoString.ListToString(uids);
			String hql = "from BxMidle b where  b.bxtype='" + type + "'"+" and b.wkState='"+wkState+"'"
					+ "  and b.uid in (" + uid + ")"+ " and ( b.dtLook >= '" + start + "' and b.dtLook <='"
							+ end + "') order by b.dtLook ";
			double cpayone = agestj.getPayone(), cpaytwo = agestj.getPaytwo(), cpayall = agestj
					.getPayall(), ctotal = agestj.getBxSum();
			List ubx = dao.find(hql);
			for (int i = 0; i < ubx.size(); i++) {
				BxMidle b = (BxMidle) ubx.get(i);
				cpayone += Math.round(b.getPayone()*100);
				cpaytwo += Math.round(b.getPaytwo()*100);
				cpayall += Math.round(b.getPayall()*100);
				ctotal += Math.round(b.getBxSum()*100);
			}
			agestj.setPayall(cpayall/100.0);
			agestj.setPayone(cpayone/100.0);
			agestj.setPaytwo(cpaytwo/100.0);
			agestj.setBxSum(ctotal/100.0);
			agesBx.put(type, agestj);
			return agesBx;
		} else {
			agesBx.put(type, agestj);
			return agesBx;
		}
	}
    public List getAgesData(List uids,String start,String end,String wkState){
    	List ages = new ArrayList();
    	ages.add(getAgeTjData(uids,"门诊",start,end,wkState));
    	ages.add(getAgeTjData(uids,"住院",start,end,wkState));
    	return ages;
    }
    
    public List getAgesDataBgf(List uids,String start,String end,String wkState){
    	BxMidle b = baogf(uids,start,end);
    	List ages = new ArrayList();
    	Map<String, BxMidle> AgeBgfTj = new HashMap<String, BxMidle>();
    	Map<String, BxMidle> AgeTjDataMz = getAgeTjData(uids,"门诊",start,end,wkState);
    	Map<String, BxMidle> AgeTjDataZy = getAgeTjData(uids,"住院",start,end,wkState);
    	if(start.contains("01-01")){
    		BxMidle mz = AgeTjDataMz.get("门诊");
    		BxMidle zy = AgeTjDataZy.get("住院");  
    		double bxsum = Math.round(mz.getBxSum()*100+zy.getBxSum()*100)/100.0;
    		b.setPayall(bxsum);
    		b.setBxSum(Math.round(bxsum*100-b.getPayone()*100)/100.0);	
    		AgeBgfTj.put("包干费", b);
    		ages.add(AgeBgfTj); 		
    	}
    	
    	ages.add(AgeTjDataMz);
    	ages.add(AgeTjDataZy);
    	
    	return ages;
    }
    
    public List getAgesTxDataBgf(List uids,String start,String end,String wkState){
    	BxMidle b =  new BxMidle(0.0, 0.0, 0.0, 0.0);
    	List ages = new ArrayList();
    	Map<String, BxMidle> AgeBgfTj = new HashMap<String, BxMidle>();
    	Map<String, BxMidle> AgeTjDataMz = getAgeTjData(uids,"门诊",start,end,wkState);
    	Map<String, BxMidle> AgeTjDataZy = getAgeTjData(uids,"住院",start,end,wkState);
    	if(start.contains("01-01")){
    		BxMidle mz = AgeTjDataMz.get("门诊");
    		BxMidle zy = AgeTjDataZy.get("住院");  
    		double bxsum = Math.round(mz.getBxSum()*100+zy.getBxSum()*100)/100.0;
    		b.setPayall(bxsum);
    		b.setBxSum(Math.round(bxsum*100-b.getPayone()*100)/100.0);	
    		AgeBgfTj.put("包干费", b);
    		ages.add(AgeBgfTj); 		
    	}
    	
    	ages.add(AgeTjDataMz);
    	ages.add(AgeTjDataZy);
    	
    	return ages;
    }
    
    //年龄统计扣包干费
    public BxMidle baogf(List uids,String start,String end){
    	BxMidle agesbagf = new BxMidle(0.0, 0.0, 0.0, 0.0);
    	double cpayone = agesbagf.getPayone(), cpaytwo = agesbagf.getPaytwo(), cpayall = agesbagf
				.getPayall(), ctotal = agesbagf.getBxSum();
    	if (uids.size() > 0) {
	    	String uid = CollectiontoString.ListToString(uids);
	
	    	String hql = "from UUser b where  b.id in (" + uid + ")";
	        List ubx = getDao().find(hql);
	    	for (int i = 0 ; i < ubx.size(); i ++){
	        	UUser u = (UUser) ubx.get(i);
	        	cpayone += Math.round(u.getBaogf()*100)/100.0;
	    	} 
	    	agesbagf.setPayone(cpayone);
    	}
    	return agesbagf;
    }
}
